package com.julian.delegatePattern.virtualProxy;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageProxyState implements Icon {
    State imageLoaded;
    State imageUnloaded;
    State currentState;

    ImageIcon imageIcon;
    URL imageURL;
    Thread retrievalThread;
    boolean retrieving = false;

    public ImageProxyState(URL imageURL) {
        this.imageURL = imageURL;
        imageLoaded = new ImageLoaded(this);
        imageUnloaded = new ImageUnloaded(this);
        currentState = imageUnloaded;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        currentState.paintIcon(c,g,x,y);
    }

    @Override
    public int getIconWidth() {
        return currentState.getIconWidth();
    }

    @Override
    public int getIconHeight() {
        return currentState.getIconHeight();
    }

    public boolean isRetrieving() {
        return retrieving;
    }

    public void setCurrentState(State currentState) {
        this.currentState = currentState;
    }

    public void setImageIcon(ImageIcon imageIcon) {
        this.imageIcon = imageIcon;
    }

    public URL getImageURL() {
        return imageURL;
    }

    public void setRetrievalThread(Thread retrievalThread) {
        this.retrievalThread = retrievalThread;
    }

    public void setRetrieving(boolean retrieving) {
        this.retrieving = retrieving;
    }

    public void startThread(){
        retrievalThread.start();
    }

    public int getIconWidthByImageIcon(){
        return imageIcon.getIconWidth();
    }

    public int getIconHeightByImageIcon(){
        return imageIcon.getIconHeight();
    }

    public void paintIconByImageIcon(Component c, Graphics g, int x, int y){
        imageIcon.paintIcon(c,g,x,y);
    }
}
